﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _1._6检测线程状态
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程开始……");
            Thread t1 = new Thread(PrintNumbersWithState);
            t1.Name = "线程t1";
            Thread t2 = new Thread(DoNothing);
            t2.Name = "线程t2";
            Console.WriteLine("主线程状态："+Thread.CurrentThread.ThreadState.ToString());
            t2.Start();
            t1.Start();
            for (int i = 0; i < 20; i++)
            {
               
                Console.Write("主线程正在遍历数字："+i+"    ");
                Console.Write(t1.Name+"状态："+t1.ThreadState.ToString()+"    ");
                Console.WriteLine(t2.Name + "状态：" + t2.ThreadState.ToString());
                Thread.Sleep(TimeSpan.FromSeconds(1));
                
            }
            Thread.Sleep(TimeSpan.FromSeconds(6));
            Console.WriteLine("准备阻断线程t1……");
            t1.Abort();

            Console.WriteLine("执行完成！");
            Console.WriteLine(t1.Name +"状态："+ t1.ThreadState.ToString());
            Console.WriteLine(t2.Name + "状态：" + t2.ThreadState.ToString());
            Console.ReadLine();
        }

        static void PrintNumbersWithState()
        {
            Console.WriteLine("t1 线程开始……");
            
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                Console.Write("t1线程正在遍历数字：" + i + "    ");
                Console.WriteLine("线程t1状态：" + Thread.CurrentThread.ThreadState.ToString());
            }
        }

        static void DoNothing() 
        {
            Console.WriteLine("t2线程沉睡10秒……");
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine("t2线程结束");
        }
    }
}
